package dbExec

import (
	"encoding/json"
	"reflect"
)

// 映射结构体值的递归函数
func mapStructValues(sourceValue reflect.Value, destinationValue reflect.Value) {
	// 检查源值和目标值的类型是否相同
	//if sourceValue.Type() != destinationValue.Type() {
	//	return
	//}
	// 解引用指针类型的值
	if sourceValue.Kind() == reflect.Ptr {
		sourceValue = sourceValue.Elem()
	}
	if destinationValue.Kind() == reflect.Ptr {
		destinationValue = destinationValue.Elem()
	}
	sinceStructSeq := 0
	// 处理结构体字段
	for i := 0; i < sourceValue.NumField(); i++ {
		sourceField := sourceValue.Type().Field(i)
		sourceFieldValue := sourceValue.Field(i)
		// 检查字段是否带有json标签
		if jsonTag := sourceField.Tag.Get("json"); jsonTag != "" {
			sinceStructSeq++
			destinationField := destinationValue.FieldByName(sourceField.Name)
			// 检查目标结构体是否存在字段
			if destinationField.IsValid() && destinationField.CanSet() {
				// 检查字段类型
				if destinationField.Type() == sourceFieldValue.Type() {
					// 设置字段值
					destinationField.Set(sourceFieldValue)
				} else {
					// 如果字段类型不匹配，尝试使用JSON进行转换
					destinationJSON, _ := json.Marshal(sourceFieldValue.Interface())
					_ = json.Unmarshal(destinationJSON, destinationField.Addr().Interface())
				}
			}
			// 如果是结构体类型，则递归处理子结构体
			if sourceFieldValue.Kind() == reflect.Struct {
				mapStructValues(sourceFieldValue, destinationValue.Field(sinceStructSeq-1))
			}
			//// 如果是指针类型，则创建新的目标指针并递归处理
			//if sourceFieldValue.Kind() == reflect.Ptr && destinationField.Kind() == reflect.Ptr {
			//	if !sourceFieldValue.IsNil() {
			//		newDestinationValue := reflect.New(destinationField.Type().Elem())
			//		mapStructValues(sourceFieldValue.Elem(), newDestinationValue.Elem())
			//		destinationField.Set(newDestinationValue)
			//	}
			//}
		}
	}
}

func reflecMap(dafs, n any) {
	mapStructValues(reflect.ValueOf(dafs), reflect.ValueOf(n).Elem())
}
func oracleRef(s, d any) any {
	reflecMap(s, d)
	return d
}
func mysqlRef(s, d any) any {
	reflecMap(s, d)
	return d
}
func clusterRef(s, d any) any {
	reflecMap(s, d)
	return d
}
func sqlserverRef(s, d any) any {
	reflecMap(s, d)
	return d
}

var (
	dbTypeInterFuncRef = map[string]reflect.Value{
		"cluster":   reflect.ValueOf(clusterRef),
		"mysql":     reflect.ValueOf(mysqlRef),
		"oracle":    reflect.ValueOf(oracleRef),
		"sqlserver": reflect.ValueOf(sqlserverRef),
	}
	//dataFixAPI      map[string]any
	metaDataAPI map[string]any
	//openDBAPI       map[string]any
	tableDataAPI map[string]any
	//hotSqlApi       map[string]any
	analyzeApi      map[string]any
	optimizationApi map[string]any
	sqlParseApi     map[string]any
)

func init() {
	//metaDataAPI = map[string]any{
	//	"mysql":     &mysql.TableMeta{},
	//	"oracle":    &oracle.TableMeta{},
	//	"cluster":   &cluster.TableMeta{},
	//	"sqlserver": &sqlserver.TableMeta{},
	//}
	//openDBAPI = map[string]any{
	//	"mysql":     &mysql.DBConnStruct{},
	//	"oracle":    &oracle.DBConnStruct{},
	//	"cluster":   &cluster.DBConnStruct{},
	//	"sqlserver": &sqlserver.DBConnStruct{},
	//}
	//tableDataAPI = map[string]any{
	//	"mysql":     &mysql.QueryTable{},
	//	"oracle":    &oracle.QueryTable{},
	//	"cluster":   &cluster.QueryTable{},
	//	"sqlserver": &sqlserver.QueryTable{},
	//}
	//hotSqlApi = map[string]any{
	//	"mysql":     &mysql.HotSql{},
	//	"oracle":    &oracle.HotSql{},
	//	"cluster":   &cluster.HotSql{},
	//	"sqlserver": &sqlserver.HotSql{},
	//}
	//analyzeApi = map[string]any{
	//	//"mysql":   &mysql.TableStruct{},
	//	//"oracle":  &oracle.HotSql{},
	//	"cluster": &cluster.Analyze{},
	//}
	//optimizationApi = map[string]any{
	//	"mysql":     &mysql.HotSql{},
	//	"oracle":    &oracle.HotSql{},
	//	"cluster":   &cluster.HotSql{},
	//	"sqlserver": &sqlserver.HotSql{},
	//}
	//sqlParseApi = map[string]any{
	//	"mysql":   &mysql.SqlParse{},
	//	"oracle":  &oracle.HotSql{},
	//	"cluster": &cluster.SqlParse{},
	//	//"sqlserver": &sqlserver.SqlParse{},
	//}
}
